﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalcSharp.MDIUI
{
    public partial class SimpleInput : Form
    {
        private StringBuilder formula;

        public SimpleInput()
        {
            InitializeComponent();
            this.formula = new StringBuilder();
            this.tbDisplay.Text = "\r\n0";
        }

        private void AddToFormula(string txt)
        {
            this.formula.Append(txt);
            string lastres = tbDisplay.Lines[1];
            this.tbDisplay.Clear();
            this.tbDisplay.Text = formula.ToString() + "\r\n" + lastres;
        }

        private void setRes(string res)
        {
            this.tbDisplay.Clear();
            this.tbDisplay.Text = this.formula.ToString() + "\r\n" + res;
        }

        private void InputClick(object sender, EventArgs e)
        {
            Button ip = (Button)sender;
            this.AddToFormula(ip.Text);
        }

        private void btnBackSpace_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbDisplay.Lines[0]))
            {
                if (formula.Length > 0)
                {
                    string ne = tbDisplay.Lines[0].Substring(0, tbDisplay.Lines[0].Length - 1);
                    this.formula.Remove(formula.Length - 1, 1); 
                    string res = tbDisplay.Lines[1];
                    this.tbDisplay.Text = ne + "\r\n" + res;
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            this.tbDisplay.Clear();
            this.tbDisplay.Text = "\r\n0";
            this.formula.Remove(0, this.formula.Length);
        }

        private void cmbFuncts_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.AddToFormula((string)this.cmbFuncts.Items[cmbFuncts.SelectedIndex]);
            this.cmbFuncts.SelectedText = "";

        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            double res;
            try
            {
                ReversePolishNotation rpn = new ReversePolishNotation();
                rpn.Parse(this.tbDisplay.Lines[0]);
                res = rpn.Evaluate();
                this.setRes(res.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void SimpleInput_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.NumPad0:
                case Keys.D0:
                    this.AddToFormula("0");
                    break;
                case Keys.NumPad1:
                case Keys.D1:
                    this.AddToFormula("1");
                    break;
                case Keys.NumPad2:
                case Keys.D2:
                    this.AddToFormula("2");
                    break;
                case Keys.NumPad3:
                case Keys.D3:
                    this.AddToFormula("3");
                    break;
                case Keys.NumPad4:
                case Keys.D4:
                    this.AddToFormula("4");
                    break;
                case Keys.NumPad5:
                case Keys.D5:
                    this.AddToFormula("5");
                    break;
                case Keys.NumPad6:
                case Keys.D6:
                    this.AddToFormula("6");
                    break;
                case Keys.NumPad7:
                case Keys.D7:
                    this.AddToFormula("7");
                    break;
                case Keys.NumPad8:
                case Keys.D8:
                    this.AddToFormula("8");
                    break;
                case Keys.NumPad9:
                case Keys.D9:
                    this.AddToFormula("9");
                    break;
            }
        }
    }
}
